using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace TmxUtil
{
    public class MSBuildUtil
    {
        public const string MSBNS = "http://schemas.microsoft.com/developer/msbuild/2003";

        public static string GetRootNamespace(XmlDocument project)
        {
            XmlNamespaceManager nsm = new XmlNamespaceManager(project.NameTable);
            nsm.AddNamespace("def", MSBuildUtil.MSBNS);
            XmlElement elNS = (XmlElement)project.DocumentElement.SelectSingleNode(".//def:PropertyGroup/def:RootNamespace", nsm);
            if (elNS != null)
                return elNS.InnerText;

            return null;
        }

        public static string GetOutputType(XmlDocument project)
        {
            XmlNamespaceManager nsm = new XmlNamespaceManager(project.NameTable);
            nsm.AddNamespace("def", MSBuildUtil.MSBNS);
            XmlElement elNS = (XmlElement)project.DocumentElement.SelectSingleNode(".//def:PropertyGroup/def:OutputType", nsm);
            if (elNS != null)
                return elNS.InnerText;

            return null;
        }

        public static string GetAssemblyName(XmlDocument project)
        {
            XmlNamespaceManager nsm = new XmlNamespaceManager(project.NameTable);
            nsm.AddNamespace("def", MSBuildUtil.MSBNS);
            XmlElement elNS = (XmlElement)project.DocumentElement.SelectSingleNode(".//def:PropertyGroup/def:AssemblyName", nsm);
            if (elNS != null)
                return elNS.InnerText;

            return null;
        }
        
        public static string GetDependsUpon(XmlDocument project, string embeddedResource)
        {
            XmlNamespaceManager nsm = new XmlNamespaceManager(project.NameTable);
            nsm.AddNamespace("def", MSBuildUtil.MSBNS);

            XmlNodeList nl = project.DocumentElement.SelectNodes("//def:ItemGroup/def:EmbeddedResource", nsm);
            foreach (XmlNode n in nl)
            {
                XmlElement el = n as XmlElement;
                if (el != null)
                {
                    string resx = el.GetAttribute("Include");
                    if (resx != null && resx.ToLower() == embeddedResource.ToLower())
                    {
                        XmlElement el2 = (XmlElement)el.SelectSingleNode("def:DependentUpon", nsm);
                        if (el2 != null)
                            return el2.InnerText;
                    }
                }
            }

            return null;
        }

        public static void AddResource(XmlDocument project, string resourceFile, string depentUpon)
        {
            XmlNamespaceManager nsm = new XmlNamespaceManager(project.NameTable);
            nsm.AddNamespace("def", MSBuildUtil.MSBNS);
            XmlElement elItemGroup = (XmlElement)project.DocumentElement.SelectSingleNode("//def:ItemGroup[def:EmbeddedResource]", nsm);

            XmlElement el1 = elItemGroup.OwnerDocument.CreateElement(string.Empty, "EmbeddedResource", MSBuildUtil.MSBNS);
            elItemGroup.AppendChild(el1);
            el1.SetAttribute("Include", resourceFile);

/*
            if (!string.IsNullOrEmpty(depentUpon))
            {
                XmlElement el3 = elItemGroup.OwnerDocument.CreateElement(string.Empty, "DependentUpon", MSBuildUtil.MSBNS);
                el3.InnerText = depentUpon;
                el1.AppendChild(el3);
            }
*/

            XmlElement el2 = elItemGroup.OwnerDocument.CreateElement(string.Empty, "SubType", MSBuildUtil.MSBNS);
            el2.InnerText = "Designer";
            el1.AppendChild(el2);
        }

        public static string[] GetEmbeddedResXList(XmlDocument doc)
        {
            List<string> list = new List<string>();
            XmlNamespaceManager nsm = new XmlNamespaceManager(doc.NameTable);
            nsm.AddNamespace("def", MSBuildUtil.MSBNS);
            XmlNodeList nl = doc.DocumentElement.SelectNodes("//def:ItemGroup/def:EmbeddedResource", nsm);
            foreach (XmlNode n in nl)
            {
                XmlElement el = n as XmlElement;
                if (el != null)
                {
                    string resx = el.GetAttribute("Include");
                    if (resx != null && resx.ToLower().EndsWith(".resx"))
                        list.Add(resx);
                }
            }

            return list.ToArray();
        }

        public static string[] GetOutputRefs(XmlDocument doc)
        {
            XmlNamespaceManager nsm = new XmlNamespaceManager(doc.NameTable);
            nsm.AddNamespace("def", MSBuildUtil.MSBNS);
            XmlNodeList nl = doc.DocumentElement.SelectNodes("//def:PropertyGroup/def:OutputPath", nsm);

            List<string> list = new List<string>();
            foreach (XmlElement n in nl)
            {
                string outputPath = n.InnerText;
                if (outputPath != null && outputPath.Trim().Length > 0)
                    list.Add(outputPath.Trim());
            }

            return list.ToArray();
        }

    }
}
